package openrr.map;

import openrr.GameMain;
import openrr.geom.TextureGroup;
import openrr.geom.Mesh;
import openrr.gl.Texture;

import java.io.File;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.*;
import java.io.IOException;

import java.util.Vector;

import openrr.map.Tile;
import openrr.utils.XMLDocument;




public class MapLoader {
	
	private TextureGroup[][] renderMap;
	private Tile[][] map;
	
	public GameMain game;
	public MapManager manager;
	
	private Texture soilMap, heightMap;
	private int[][] RGBValues;
	private TextureGroup[] groups;
	
	public MapLoader(GameMain gameMain, MapManager mapManager)
	{
		map = mapManager.map;
		game = gameMain;
		manager = mapManager;
	}
	
	public void loadMap(String url)
	{
		XMLDocument doc = new XMLDocument("src/res/sampleMap.xml");
		
		parsePackages();
		soilMap = new Texture(doc.getAttribute("/ORRmap/maps/soilMap", "src"));
		heightMap = new Texture(doc.getAttribute("/ORRmap/maps/heightMap", "src"));
		this.manager.width = heightMap.width;
		this.manager.height = heightMap.height;
		map = new Tile[heightMap.width][heightMap.height];
		this.manager.map = map;
		for(int i = 0; i < heightMap.width; i++)
		{
			for(int j = 0; j < heightMap.height; j++)
			{
				map[i][j] = new Tile(this.manager, this.manager.map, i, j);
			}
		}
		parseHeightMap();
		parseSoilMap();
		
		for(int i = 0; i < heightMap.width; i++)
		{
			for(int j = 0; j < heightMap.height; j++)
			{
				map[i][j].updateGeometry();
			}
		}
	}
	
	private void parseHeightMap()
	{
		Texture map = this.heightMap;
		int[] nullPixel = new int[4];
		nullPixel[0] = 0;
		nullPixel[1] = 0;
		nullPixel[2] = 0;
		nullPixel[3] = 255;
		int[] pixel;
		for(int i = 0; i < map.width; i++)
		{
			for(int j = 0; j < map.height; j++)
			{
				pixel = map.getPixelAt(i, j);
				//System.out.println("setting (" + pixel[0] + ", " + pixel[1] + ", " + pixel[2] + ", " + pixel[3] + ")");
				if((pixel[0] == nullPixel[0]) && (pixel[1] == nullPixel[1]) && (pixel[2] == nullPixel[2]) && (pixel[3] == nullPixel[3]))
				{
					//wall
					this.map[i][j].setHeight(1);
				} else {
					//ground
					this.map[i][j].setHeight(0);
				}
			}
		}
	}
	
	private void parseSoilMap()
	{
		Texture map = this.soilMap;
		for(int i = 0; i < map.width; i++)
		{
			for(int j = 0; j < map.height; j++)
			{
				this.map[i][j].setTileSet(this.manager.soilManager.getTileSetByColour(map.getPixelAt(i, j)));
			}
		}
	}
	private void parsePackages()
	{
		SoilManager manager = this.manager.soilManager;
		XMLDocument doc = new XMLDocument("src/res/defaultRock.xml");
		//System.out.println("# of childs: " + doc.getNumChilds("/ORRPack/tileSets/tileSet"));
		int childs = doc.getNumChilds("/ORRPack/tileSets/tileSet");
		RGBValues = new int[childs][4];
		groups = new TextureGroup[childs];
		TextureGroup currentGroup;
		TileSet set;
		int[] colour;
		String setType;
		String prefix = doc.getAttribute("/ORRPack/config/pathPrefix", "value");
		for(int i = 0; i < childs; i++)
		{
			//trace("parsing child: " + i);
			setType = doc.getAttribute("/ORRPack/tileSets/tileSet[" + (i+1) + "]", "type");
			set = new TileSet(setType);
			colour = new int[3];
			colour[0] = Integer.parseInt(doc.getAttribute("/ORRPack/tileSets/tileSet[" + (i+1) + "]", "r"));
			colour[1] = Integer.parseInt(doc.getAttribute("/ORRPack/tileSets/tileSet[" + (i+1) + "]", "g"));
			colour[2] = Integer.parseInt(doc.getAttribute("/ORRPack/tileSets/tileSet[" + (i+1) + "]", "b"));
			for(int j = 0; j < doc.getNumChilds("/ORRPack/tileSets/tileSet[" + (i+1) + "]/texture"); j++)
			{
				currentGroup = new TextureGroup(prefix + doc.getAttribute("/ORRPack/tileSets/tileSet[" + (i+1) + "]/texture[" + (j+1) + "]", "src"));
				this.manager.mapMesh.addGroup(currentGroup);
				set.addTexture(currentGroup, doc.getAttribute("/ORRPack/tileSets/tileSet[" + (i+1) + "]/texture[" + (j+1) + "]", "type"));
			}
			manager.addTileSet(setType, colour, set);
		}
	}
	private void trace(String line)
	{
		System.out.println(line);
	}
}
